package com.taoyibao.hongcanting.ui.home.delegate;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author TangAnna
 * @description: 选择就餐人数
 * @date :${DATA} 11:55
 */
public class NumberPeopleDelegate extends BackToolbarDelegate implements TextWatcher {

    private TagFlowLayout mTagFlowLayout;
    private List<String> mData;
    private EditText mEtInputAmount;
    private TagAdapter mAdapter;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_number_people;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("就餐人数");
        mTagFlowLayout = get(R.id.tfl_number_list);
        mEtInputAmount = get(R.id.et_number_inputAmount);
        mEtInputAmount.addTextChangedListener(this);
        mData = new ArrayList<>();
        setSeat();
    }

    /**
     * 设置选择人数的数据
     */
    public void setAmountData(int nub) {
        for (int i = 1; i < nub + 1; i++) {
            mData.add(i + "");
        }
        mTagFlowLayout.setAdapter(mAdapter = new TagAdapter<String>(mData) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView textView = (TextView) getActivity().getLayoutInflater().inflate(R.layout.item_number_people, null);
                textView.setText(s);
                return textView;
            }
        });
        mTagFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                if (selectPosSet != null && selectPosSet.size() > 0) {
                    mEtInputAmount.setText("");
                }
            }
        });
    }

    /**
     * 设置桌号
     */
    public void setSeat() {
        ((TextView) get(R.id.tv_number_seat)).setText(SpUtils.getSeatName());
    }

    /**
     * 获取选择的就餐人数
     *
     * @return
     */
    public int getSelectAmount() {
        if (!TextUtils.isEmpty(mEtInputAmount.getText().toString()) && !TextUtils.equals(mEtInputAmount.getText().toString(), "0")) {//先判断是否有输入值
            return Integer.parseInt(mEtInputAmount.getText().toString());
        } else if (!TextUtils.isEmpty(getTagFlowLayoutChecked())) {//判断是否选择固定人数
            return Integer.parseInt(getTagFlowLayoutChecked());
        } else {
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintNumberPeople));
        }
        return 0;
    }

    /**
     * 获取选择的人数值
     */
    public String getTagFlowLayoutChecked() {
        Set<Integer> selectedList = mTagFlowLayout.getSelectedList();
        if (selectedList != null && selectedList.size() > 0) {
            Iterator<Integer> iterator = selectedList.iterator();
            if (iterator.hasNext()) {
                Integer next = iterator.next();
                return mData.get(next);
            }
        }
        return null;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(mEtInputAmount.getText().toString().trim())) {
            //清除所有选择状态
            mTagFlowLayout.onChanged();
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(mEtInputAmount.getText().toString().trim())) {
            String input = mEtInputAmount.getText().toString().trim();
            //判断是否是以0开头的数值
            if (input.startsWith("0")) {
                mEtInputAmount.setText("");
            }
        }
    }
}
